<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Virtual DOM</title>
    <!-- 加载React库 -->
    <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <!-- 加载Babel编译器 -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      const jsx = (
        <div>
          <h1></h1>
          <h2 id="title"></h2>
          <ul id="list"></ul>
        </div>
      );

      function render(vdom, container) {
        console.log(vdom)
        const element = document.createElement(vdom.type);
        Object.keys(vdom.props)
          .filter((propName) => propName !== "children")
          .forEach((propName) => (element[propName] = vdom.props[propName]));

        if (Array.isArray(vdom.props.children)) {
          vdom.props.children.forEach((child) => render(child, element));
        }
        container.appendChild(element);
      }

      render(jsx, document.getElementById("root"));
    </script>
  </body>
</html>
